/**
 * Dialog
 */
class Dialog {
    constructor(options = {}) {
        this.options = $.extend({}, options);
        this.$dialog = $('#dialog-container');
        this.$model = this.$dialog.find('.modal-dialog')
        this.$body = this.$dialog.find('.modal-body');
        this.$title = this.$dialog.find('.modal-title');
        this.$footer = this.$dialog.find('.modal-footer')
        this.default = {
            title: 'title',
            content: 'Hi',
            width: false, //默认自适应
            positoin: 'top', //模态框位置
            animationType: 'modal-primary in', //出现消失 动画类型
            titlebgColor: '#f3f7f9', //标题栏背景色
            titleWord: '#37474f', //标题字体颜色
            titleClose: '#a9a9a9', //关闭图标按钮
            confirmBtnName: '保存', //空或者不写，默认关闭模态框
            cancelBtnName: '取消'
        }
    }

    init() {
        this.event();
    }

    event() {
        let self = this;
        self.$dialog.unbind('show.bs.modal').bind('show.bs.modal', function () {
            let dialog = $(this).find('.modal-dialog').data().dialog;
            setTimeout(() => {
                u.sHeight();
                if (dialog.options.init_callback) {
                    dialog.options.init_callback.call(dialog);
                }
            },100)
        }).unbind('shown.bs.modal').bind('shown.bs.modal', function () {
            let dialog = $(this).find('.modal-dialog').data().dialog;
            // dialog.$body.niceScroll($.po('niceScroll'));
        }).unbind('hidden.bs.modal').bind('hidden.bs.modal', function () {
            let dialog = $(this).find('.modal-dialog').data().dialog;
            dialog.destroy();
        });

        this.$submitBtn = this.$dialog.find('.modal-footer .save_btn');
        this.$submitBtn.unbind('click.dialog').bind('click.dialog', function () {
            var $form = self.$dialog.find('form:eq(0)');
            if ($form.length) {
                $form.submit();
            }
        })
    }

    show() {
        this.getContent((e) => {
            this.$title.text(this.options.title || this.default.title);
            this.$body.children().detach();
            this.$body.html(e.removeClass('hide'));
            this.$model.removeAttr('style')
            this.$model.data('dialog', this);
            if (this.options.width) {
                this.$model.css({
                    width: this.options.width
                })
            }
            this.resetForm(e);
            this.$dialog.modal('show');
            //判断是否显示footer
            if (e.find('.footer').length && !e.find('.footer').hasClass('hide')) {
                this.$footer.addClass('hide');
            } else {
                this.$footer.removeClass('hide');
            }
        })

    }

    close() {
        this.$dialog.modal('hide');
    }

    getContent(callback) {
        if (this.options.container_selector) {
            if($(this.options.container_selector).length)
                dialogContainerTemp[this.options.container_selector] = $(this.options.container_selector);
            callback(dialogContainerTemp[this.options.container_selector]);
        } else if (this.options.url) {
            wdAjax.get({
                url: this.options.url,
                data: { 'source_type': 'html' },
                success(html) {
                    callback(html);
                }
            })
        }
    }

    resetForm(e) {
        if (e.parent().find('form:eq(0)').length) {
            if (e.parent().find('form:eq(0)').data().formValidation) {
                e.parent().find('form:eq(0)').data().formValidation.resetForm();
            }
            e.parent().find('form:eq(0)')[0].reset();
        }
    }

    destroy() {
        // this.$dialog.remove();
    }

}

let dialogContainerTemp = {};